/*
 * @Author: 曹捷
 * @Date: 2020-06-13 10:54:38
 * @LastEditors: 曹捷
 * @LastEditTime: 2020-11-17 22:06:21
 * @Description: 系统工具类 Mix
 */
import { mapGetters } from 'vuex'
import { toLogin, loginOut } from '@/app-modules/utils/user'

export default {
  computed: {
    ...mapGetters(['userInfo']),
    hasLogin () {
      return this.userInfo.userId ? true : false
    },
    /**
     * 普通员工
     */
    userCommon () {
      return this.userInfo.userType && this.userInfo.userType === 1
    },
    /**
     * 专家
     */
    userZj () {
      return this.userInfo.userType && this.userInfo.userType === 2
    },
  },
  methods: {
    // input 数值输入  绑定data
    onKeyInput: function (event, inputEvent) {
      let arr = inputEvent.split('.')
      let item = this
      let key = arr.pop()
      arr.length > 0 &&
        arr.forEach((element) => {
          item = item[element]
        })
      this.$set(item, key, event.detail.value)
    },
    /**
     * 上传图片
     * @param {int} type
     * 1 原图
     * 2 压缩
     * 3 原图和压缩图
     */
    uploadImg (options) {
      // uploadFile
      // uploadType
      let data = {}
      console.log('Ob', Object.prototype.toString.call(options).indexOf('Object'));
      let imgOptions = {
        count: 9
      }
      if (Object.prototype.toString.call(options).indexOf('Object') !== -1) {
        if (options.count) {
          imgOptions.count = options.count
        }
      } else {
        data = { uploadType: options ? options : 1 }
      }
      // let data = { uploadType: options ? options : 1 }
      uni.chooseImage({
        count: imgOptions.count,
        success: (chooseImageRes) => {
          console.log('chooseImageRes ', chooseImageRes)
          const tempFilePaths = chooseImageRes.tempFilePaths
          uni.showLoading({
            title: '上传中',
          })
          let num = 0
          for (let i = 0; i < tempFilePaths.length; i++) {
            this.$http
              .uploadFileByType(tempFilePaths[i], data)
              .then((res) => {
                console.log('uploadImg -> res', res)
                this.imgSuccess(res)
              })
              .finally((info) => {
                console.log('info -> res', info)
                num++
                // console.log('uploadImg -> finally', num, tempFilePaths.length, num === tempFilePaths.length)
                if (num === tempFilePaths.length) {
                  uni.hideLoading()
                }
              })
          }
        },
      })
    },
    loginTo (url) {
      if (this.hasLogin) {
        this.$app.goto(url)
      } else {
        toLogin()
      }
    },
    // 图片上传成功回调
    imgSuccess () { },
  },
}
